/* eslint-disable prefer-promise-reject-errors */
import config from '@/config/index'
import cloudbase from "@cloudbase/js-sdk";

export default {
    install(wf) {

        console.log('config---------------', config);

        const app = cloudbase.init({
            env: config.envId
        });

        window.cloud = app

        const auth = app.auth({
            persistence: "local"
        });

        const user = auth.currentUser;

        const validateEmailAndPassword = (email, password) => {
            let reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            let isEmail = reg.test(email);

            if (!email || !password || !isEmail) {
                return { message: '请检查邮箱或密码格式是否正确' }
            }

            if (password.length < 8) {
                return { message: '密码长度过短' }
            }

            return {
                status: true,
            }
        }

        const m = {
            // 校验邮箱密码
            validateEmailAndPassword,

            // 检查登录态
            hasLoginState() {
                return auth.hasLoginState()
            },

            // 邮箱注册
            signUpWithEmailAndPassword(email, password) {
                return new Promise((reslove, reject) => {

                    let validateResult = validateEmailAndPassword(email, password)

                    if (validateResult.message) {
                        reject(validateResult);
                    }

                    // 邮箱验证
                    if (validateResult.status) {
                        app
                            .auth()
                            .signUpWithEmailAndPassword(email, password).then(res => {
                                console.log(res);
                                reslove(res)
                            }).catch(err => {
                                reject({
                                    err,
                                    message: '邮箱注册失败',
                                })
                            })
                    }

                })

            },

            // 邮箱登录
            signInWithEmailAndPassword(email, password) {
                return new Promise((resolve, reject) => {
                    app
                        .auth()
                        .signInWithEmailAndPassword(email, password).then(res => {
                            let user = res.user
                            this.update(user)
                            resolve(user)
                        }).catch(err => {
                            reject(err)
                        })
                })

            },

            signOut() {
                return auth.signOut();
            },

            updateUser(userInfo) {
                console.log(userInfo);
                return new Promise((reslove, reject) => {

                    if (!userInfo || typeof userInfo !== 'object') {
                        reject()
                    }

                    user.update(userInfo).then(res => {
                        reslove(res)
                    }).catch(err => {
                        reject(err)
                    })

                })
            },

            getUserInfo() {
                return auth.currentUser
            },

            update(u) {
                let user = u || this.getUserInfo();
                if (!user || !user.avatarUrl || user.avatarUrl.length === 0) {
                    return
                }
                app.getTempFileURL({
                    fileList: [user.avatarUrl]
                }).then(res => {
                    res.fileList.forEach(el => {
                        if (el.code === "SUCCESS") {
                            let avatarUrl = el.tempFileURL;
                            wf.getModule('store').commit('updateUserInfo', {
                                avatarImg: avatarUrl,
                                ...user
                            })
                            console.log('avatarUrl', avatarUrl);
                        }
                    });
                });
            }


        }

        // 获取头像图片
        m.update()




        wf.addModule('login', m);
    },
};
